#include "chatframe.h"
#include "ui_chatframe.h"

ChatFrame::ChatFrame(QTcpSocket * s,QString f,QString t,QWidget *parent) :
    socket(s),
    from(f),
    to(t),
    QDialog(parent),
    ui(new Ui::ChatFrame)
{
    ui->setupUi(this);
    connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(sendmsg()));
}

ChatFrame::~ChatFrame()
{
    delete ui;
}

void ChatFrame::setContent(const char *content)
{

    ChatPackage * pack = (ChatPackage *)content;
    if(pack->msg_type == 15)
        return;

    qDebug()<<pack->from;
    qDebug()<<pack->msgbody;
    QString str1(pack->from);

    QString str2(pack->msgbody);

    ui->textBrowser_recvmsg->append("<font color=\"#FF0000\">"+str1+ "</font> "+"\r\n");
    ui->textBrowser_recvmsg->append("<font color=\"#F2111E\">"+str2+ "</font> "+"\r\n");
}

void ChatFrame::sendmsg()
{
    ChatPackage pack;
   // pack.to
    sprintf(pack.from,from.toStdString().c_str());
    sprintf(pack.to,to.toStdString().c_str());
    sprintf(pack.msgbody,ui->textEdit->toPlainText().toStdString().c_str());

    qDebug()<<pack.from;
    qDebug()<<pack.to;
    qDebug()<<pack.msgbody;

    pack.msg_type = 15;

    socket->write((const char *)&pack,MSG_LENGTH);
}
